package com.tesco.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tesco.entity.domin.YgUser;
import com.tesco.service.YgUserService;
import com.tesco.utils.TokenUtils;
import com.tesco.utils.WebApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author jc
 */
@Api(tags = "用户模块")
@RequestMapping("/yguser")
@RestController
@CrossOrigin(origins = "*")
public class UserController {

    @Resource
    private YgUserService ygUserService;


    /**
     * 用户注册
     * @param username
     * @param password
     * @return
     */
    @ApiOperation("用户注册")
    @PostMapping("/userRegister")
    public WebApiResult userRegister(@RequestParam("username") String username,@RequestParam("password") String password,@RequestParam("usertype") String usertype){
        try {
            if (ygUserService.userRegisterInterface(username, password,usertype)) {
                return WebApiResult.ok("注册成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return WebApiResult.error("用户名已被注册,请重新选择用户名");
    }

    @ApiOperation("用户登录")
    @PostMapping("/userLogin")
    public WebApiResult<String> userLogin(@RequestParam("username") String username, @RequestParam("password") String password){
        try {
            YgUser ygUser = ygUserService.userLoginInterface(username, password);
            if (ygUser != null) {
                return new WebApiResult<>(200, true, "success",  TokenUtils.token(username),0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return WebApiResult.error("登录失败");
    }

    @ApiOperation("用户更新(token)")
    @PostMapping("/userUpdate")
    public WebApiResult userUpdate(HttpServletRequest request,YgUser ygUser){
        try {
            String username = TokenUtils.getUsername(request);
            ygUserService.userUpdate(username,ygUser);
            return WebApiResult.ok("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return WebApiResult.error("更新失败");
        }
    }

    @ApiOperation("获取所有用户")
    @PostMapping("/getAllUser")
    public WebApiResult<List<String>> getAllUser(){
        try {
            List<String> collect = ygUserService.list().stream().map(a -> a.getUsername()).collect(Collectors.toList());
            return WebApiResult.ok(collect);
        } catch (Exception e) {
            e.printStackTrace();
            return WebApiResult.error();
        }
    }

    @ApiOperation("用户信息(token)")
    @GetMapping("/userInfo")
    public WebApiResult<YgUser> userInfo(HttpServletRequest request){
        try {
            String username = TokenUtils.getUsername(request);
            YgUser ygUser = ygUserService.userInfo(username);
            if (ygUser != null) {
                return WebApiResult.ok(ygUser);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return WebApiResult.error("查询失败");
    }


    @PostMapping("/addBalance")
    @ApiOperation("余额充值(token)")
    public WebApiResult addBalance(HttpServletRequest request,@RequestParam("balance") int balance){
        try {
            BigDecimal bigDecimal = new BigDecimal(balance);
            String username = TokenUtils.getUsername(request);
            YgUser ygUser = ygUserService.userInfo(username);
            UpdateWrapper<YgUser> ygUserUpdateWrapper = new UpdateWrapper<>();
            ygUserUpdateWrapper.set(YgUser.COL_BALANCE,ygUser.getBalance().add(bigDecimal))
                    .eq(YgUser.COL_USERNAME,username);
            ygUserService.update(ygUserUpdateWrapper);
            return WebApiResult.ok("充值成功");
        } catch (Exception e) {
            e.printStackTrace();
            return WebApiResult.error("充值失败");
        }
    }


}
